package com.hkts.naturalgas.dictionaries.controller;

import com.hkts.naturalgas.dictionaries.entity.HktsEnvironmentProtectionDictionaries;
import com.hkts.naturalgas.dictionaries.entity.HktsEnvironmentProtectionItem;
import com.hkts.naturalgas.dictionaries.service.IHktsEnvironmentProtectionDictionariesService;
import com.hkts.naturalgas.dictionaries.service.IHktsEnvironmentProtectionItemService;
import com.hkts.naturalgas.dictionaries.utils.DBUtils;
import com.hkts.naturalgas.utils.ExcelPortUtil;
import com.hkts.naturalgas.utils.Result;
import com.hkts.naturalgas.utils.ResultUtil;

import org.apache.poi.ss.usermodel.*;
import org.apache.poi.ss.util.CellRangeAddress;
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.OutputStream;
import java.text.ParseException;
import java.util.List;
import java.util.Map;

/**
 * @ClassNameExportController
 * @Description
 * @Author yck
 * @Date2021-06-23 16:09
 * @Version V1.0
 **/
@RestController
@RequestMapping("/instrumentdata/exportData")
public class ExportDataController {
    @Autowired
    public IHktsEnvironmentProtectionDictionariesService iHktsEnvironmentProtectionDictionariesService;
    @Autowired
    public IHktsEnvironmentProtectionItemService iHktsEnvironmentProtectionItemService;

    @Autowired
    public  DBUtils dBUtils;
    @ResponseBody
    @GetMapping("/exportFile")
    /** 功能描述:
    *[date 日期, section 早班晚班 1 早班 2 晚班, workshop 车间 1是 煅烧， 2 是焙烧, response]
    * @return: com.hkts.naturalgas.utils.Result
    * @Author: 杨春科
    * @Date: 2021-06-24 10:21
    * @Description //TODO 
    */
    public Result exportData(String date,String section,String workshop, HttpServletResponse response) throws Exception {
        HktsEnvironmentProtectionDictionaries hktsEnvironmentProtectionDictionaries=new HktsEnvironmentProtectionDictionaries();
        hktsEnvironmentProtectionDictionaries.setIsExport("1");
        hktsEnvironmentProtectionDictionaries.setIsEnable("1");
        hktsEnvironmentProtectionDictionaries.setWorkShop(workshop);
        List<HktsEnvironmentProtectionDictionaries> hktsEnvironmentProtectionDictionaries1 = iHktsEnvironmentProtectionDictionariesService.selectList(hktsEnvironmentProtectionDictionaries);
        XSSFWorkbook wb = new XSSFWorkbook();
        for(int i=0;i<hktsEnvironmentProtectionDictionaries1.size();i++){
            HktsEnvironmentProtectionItem item=new HktsEnvironmentProtectionItem();
            item.setTableId(hktsEnvironmentProtectionDictionaries1.get(i).getId());
            item.setIsEnable("1");
            item.setIsExport("1");
            List<HktsEnvironmentProtectionItem> hktsEnvironmentProtectionItems = iHktsEnvironmentProtectionItemService.selectList(item);

            List<Map<String, Object>> list = dBUtils.getList(hktsEnvironmentProtectionDictionaries1.get(i), hktsEnvironmentProtectionItems,date,section);

            XSSFSheet sheet = wb.getSheet(hktsEnvironmentProtectionDictionaries1.get(i).getTableDescribe());
            wb=  ExcelPortUtil.getSheet(wb,hktsEnvironmentProtectionItems,list,hktsEnvironmentProtectionDictionaries1.get(i).getTableDescribe());
        }
        if(workshop.equals("1")){
            workshop="煅烧车间";
        }else {
            workshop="焙烧车间";
        }
        // 输出Excel文件
        OutputStream output = response.getOutputStream();
        response.reset();
        // 设置文件头
        response.setHeader("Content-Disposition",
                "attchement;filename=" + new String((workshop + ".xls").getBytes("gb2312"), "ISO8859-1"));
        response.setContentType("application/msexcel");
        wb.write(output);
        wb.close();
        return null;
    }
}
